package com.moo2u2.android.oaktononthego;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.moo2u2.android.oaktononthego.crm.CRMActivity;
import com.moo2u2.android.oaktononthego.locate.LocateColleagueActivity;
import com.moo2u2.android.oaktononthego.prefs.EditPreferences;
import com.moo2u2.android.oaktononthego.prefs.EditPreferencesHC;
import com.moo2u2.android.oaktononthego.timesheet.TimesheetActivity;
import com.moo2u2.android.oaktononthego.timesheet.TimesheetPreferences;

public class OaktonOnTheGoActivity extends Activity {

	// TODO put strings in resources
	// TODO Use CursorLoaders

	private static final int EDIT_PREFS = Menu.FIRST + 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.main);
		if (customTitleSupported)
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
					R.layout.window_title);

		((ImageView) findViewById(R.id.locateBtn))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startLocate();
					}
				});
		((TextView) findViewById(R.id.locateBtnTxt))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startLocate();
					}
				});

		((ImageView) findViewById(R.id.crmBtn))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startCRM();
					}
				});
		((TextView) findViewById(R.id.crmBtnTxt))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startCRM();
					}
				});

		((ImageView) findViewById(R.id.timesheetBtn))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startTimesheets();
					}
				});
		((TextView) findViewById(R.id.timesheetBtnTxt))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startTimesheets();
					}
				});

		((ImageView) findViewById(R.id.likeBtn))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Toast.makeText(OaktonOnTheGoActivity.this,
								"Pick this app to have this implemented!",
								Toast.LENGTH_SHORT).show();
					}
				});

		((ImageView) findViewById(R.id.plusoneBtn))
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Toast.makeText(OaktonOnTheGoActivity.this,
								"Oakton needs a Google+ account!",
								Toast.LENGTH_SHORT).show();
					}
				});
	}

	private void startTimesheets() {
		// TODO check for login details
		boolean loginDetails = true;
		if (loginDetails)
			startActivity(new Intent(OaktonOnTheGoActivity.this,
					TimesheetActivity.class));
		else
			startActivity(new Intent(OaktonOnTheGoActivity.this,
					TimesheetPreferences.class));

	}

	private void startCRM() {
		startActivity(new Intent(OaktonOnTheGoActivity.this, CRMActivity.class));
	}

	private void startLocate() {
		startActivity(new Intent(OaktonOnTheGoActivity.this,
				LocateColleagueActivity.class));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, EDIT_PREFS, Menu.NONE, "Edit Prefs")
				.setIcon(android.R.drawable.ic_menu_preferences)
				.setAlphabeticShortcut('e');

		return (super.onCreateOptionsMenu(menu));
	}

	/** {@inheritDoc} */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case EDIT_PREFS:
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				startActivity(new Intent(this, EditPreferences.class));
			} else {
				startActivity(new Intent(this, EditPreferencesHC.class));
			}
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}